package api;

import model.Article;
import model.ArticleDao;
import model.User;
import model.UserDao;
import view.HtmlGenerator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class ArticleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.验证用户是否已经登录，若没有登录要提示先登录
        resp.setContentType("text/html;charset=utf-8");
        HttpSession httpSession = req.getSession(false);
        if(httpSession == null){
            //当前未登录
            String html = HtmlGenerator.getMessagePage("请先登录","login.html");
            resp.getWriter().write(html);
            return;
        }
        User user = (User)httpSession.getAttribute("user");
        //2.判断请求中是否含有articleId参数
        String articleIdStr = req.getParameter("articleId");
        if(articleIdStr == null){
            //没有这个参数，就执行获取文章列表操作
            getAllArticle(user,resp);
        }else{
            //有这个参数，就获取文章的具体内容
            getOneArticle(Integer.parseInt(articleIdStr),user,resp);
        }
    }
    //获取文章列表
    private void getAllArticle(User user,HttpServletResponse resp) throws IOException {
        //1.查找数据库
        ArticleDao articleDao = new ArticleDao();
        List<Article> articles = articleDao.selectAll();
        // 2. 构造页面
        String html = HtmlGenerator.getArticleListPage(articles, user);
        resp.getWriter().write(html);
    }

    //获取具体文章信息
    private void getOneArticle(int articleId,User user,HttpServletResponse resp) throws IOException {
        //1.查找数据库
        ArticleDao articleDao = new ArticleDao();
        Article article = articleDao.selectById(articleId);
        if(article == null){
            //文章未找到
            String html = HtmlGenerator.getMessagePage("文章不存在","article");
            resp.getWriter().write(html);
        }
        //2.根据ID找到作者信息
        UserDao userDao = new UserDao();
        User author = userDao.selectById(article.getUserId());
        //3.构造页面
        String html = HtmlGenerator.getArticleDetailPage(article, user, author);
        resp.getWriter().write(html);
    }

    //新增文章
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //1.判断用户的登录状态，如果未登录，则提示用户登录
        HttpSession httpSession = req.getSession(false);
        if(httpSession == null){
            //未登录
            String html = HtmlGenerator.getMessagePage("请先登录","login.html");
            resp.getWriter().write(html);
            return;
        }

        User user = (User)httpSession.getAttribute("user");
        // 2. 从请求中读取浏览器提交的数据(title, content), 并进行简单校验
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        if(title == null || "".equals(title)
        || content == null || "".equals(content)){
            String html = HtmlGenerator.getMessagePage("提交的标题或者正文为空",
                    "article");
            resp.getWriter().write(html);
            return;
        }

        //3.把数据插入数据库中
        ArticleDao articleDao = new ArticleDao();
        Article article = new Article();
        article.setContent(content);
        article.setTitle(title);
        article.setUserId(user.getUserId());
        articleDao.add(article);

        //4.返回一个插入成功的页面
        String html = HtmlGenerator.getMessagePage("发布成功！","article");
        resp.getWriter().write(html);
        return;
    }
}
